<template>
  <!-- 分配权限的弹层 -->
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      show-checkbox
      check-strictly
      default-expand-all
      :data="list"
      :props="{label:'name'}"
    />
    <el-row type="flex" justify="end">
      <el-button size="mini" type="primary" @click="$emit('success')">取消</el-button>
      <el-button size="mini" type="primary" @click="hSubmit">确定</el-button>
    </el-row>
  </div>

</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTreeData } from '@/utils/index'
import { getRolesDetail, assignPremission } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async  hSubmit() {
      // 获取当前选中的树节点的id
      const permIds = this.$refs.tree.getCheckedKeys()
      console.log(permIds)
      // 修改成功
      const res = await assignPremission({ id: this.curId, permIds: permIds })
      console.log(res, '修改成功')
      this.$emit('success')
    },
    // 获取所有的权限的数据
    async  loadPermissionList() {
      try {
        const res = await getPermissionList()
        const detail = await getRolesDetail(this.curId)
        console.log(detail, 'detail')
        console.log(res, '获取权限列表')
        // 获取id 和多选框关联选中
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        // 平铺数据转化成树形数据
        this.list = transDataToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
